local skel = fk.CreateSkill {
  name = "emo__kubaiwang",
  tags = {Skill.Compulsory},
  dynamic_desc = function (self, player, lang)
    local args = {}
    local choices = {"emo__kubaiwang_maxhp", "emo__kubaiwang_hp", "emo__kubaiwang_card"}
    for _, choice in ipairs(choices) do
      local str = Fk:translate(choice, lang)
      if player:getMark(choice) == 0 then
        str = "<font color='grey'> " .. str .. " </font>"
      end
      table.insert(args, str)
    end
    return "emo__kubaiwang_inner:" .. table.concat(args, ":")
  end,
}

Fk:loadTranslationTable{
  ["emo__kubaiwang"] = "枯败的王",
  [":emo__kubaiwang"] = "锁定技，你的体力上限、体力值、初始手牌数+1；有两者失去后，失去最后一者。",

  [":emo__kubaiwang_inner"] = "锁定技，你的{1}、{2}、{3}+1；有两者失去后，失去最后一者。",

  ["@@emo__kubaiwang-inhand"] = "枯败王",
  ["emo__kubaiwang_hp"] = "体力值",
  ["emo__kubaiwang_maxhp"] = "体力上限",
  ["emo__kubaiwang_card"] = "初始手牌",
}

skel:addEffect(fk.GameStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "emo__kubaiwang_maxhp", 1)
    room:setPlayerMark(player, "emo__kubaiwang_hp", 1)
    room:changeMaxHp(player, 1)
    if not player.dead then
      room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
    end
  end,
})

--- 令我初始手牌+1，不发动技能
skel:addEffect(fk.DrawInitialCards, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_refresh = function (self, event, target, player, data)
    data.num = data.num + 1
  end,
})

--- 记录我的初始手牌
skel:addEffect(fk.AfterDrawInitialCards, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true) and target == player
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local hand = player:getCardIds("h")
    if #hand == 0 then return end
    room:setCardMark(Fk:getCardById(hand[#hand]), "@@emo__kubaiwang-inhand", player.id)
    room:setPlayerMark(player, "emo__kubaiwang_cardData", {hand[#hand]}) -- 仅记录右手第一张
    room:setPlayerMark(player, "emo__kubaiwang_card", 1)
  end,
})

---@param player ServerPlayer
---@param choice string
local loseInitial = function (player, choice)
  local room = player.room
  room:setPlayerMark(player, choice, 0)
  if choice == "emo__kubaiwang_maxhp" then
    room:changeMaxHp(player, -1)
  elseif choice == "emo__kubaiwang_hp" then
    room:loseHp(player, 1, skel.name)
  elseif choice == "emo__kubaiwang_card" then
    local mark = player:getTableMark("emo__kubaiwang_cardData")
    local cards = table.filter(player:getCardIds("h"), function (id)
      return table.contains(mark, id)
    end)
    if #cards > 0 then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonJustMove, skel.name, nil, true)
    end
  end
end

-- 扣减体力后，认为我失去了初始体力
skel:addEffect(fk.HpChanged, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("emo__kubaiwang_hp") ~= 0 and data.num < 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "emo__kubaiwang_hp", 0)
    local maxhpCheck, cardCheck = player:getMark("emo__kubaiwang_maxhp") ~= 0, player:getMark("emo__kubaiwang_card") ~= 0
    if maxhpCheck and not cardCheck then
      loseInitial(player, "emo__kubaiwang_maxhp")
    elseif cardCheck and not maxhpCheck then
      loseInitial(player, "emo__kubaiwang_card")
    end
  end,
})

-- 扣减体力上限后，认为我失去了初始体力上限
skel:addEffect(fk.MaxHpChanged, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("emo__kubaiwang_maxhp") ~= 0 and data.num < 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "emo__kubaiwang_maxhp", 0)
    local hpCheck, cardCheck = player:getMark("emo__kubaiwang_hp") ~= 0, player:getMark("emo__kubaiwang_card") ~= 0
    if hpCheck and not cardCheck then
      loseInitial(player, "emo__kubaiwang_hp")
    elseif cardCheck and not hpCheck then
      loseInitial(player, "emo__kubaiwang_card")
    end
  end,
})

-- 失去最后的初始手牌后，消除记录
skel:addEffect(fk.AfterCardsMove, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if not player.dead and player:getMark("emo__kubaiwang_card") ~= 0 then
      local mark = player:getTableMark("emo__kubaiwang_cardData")
      if table.every(mark, function (id)
        return not table.contains(player:getCardIds("h"), id)
      end) then
        for _, move in ipairs(data) do
          if move.from == player then
            for _, info in ipairs(move.moveInfo) do
              if table.contains(mark, info.cardId) and info.fromArea == Card.PlayerHand then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "emo__kubaiwang_card", 0)
    local hpCheck, maxhpCheck = player:getMark("emo__kubaiwang_hp") ~= 0, player:getMark("emo__kubaiwang_maxhp") ~= 0
    if hpCheck and not maxhpCheck then
      loseInitial(player, "emo__kubaiwang_hp")
    elseif maxhpCheck and not hpCheck then
      loseInitial(player, "emo__kubaiwang_maxhp")
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "emo__kubaiwang_maxhp", 0)
  player.room:setPlayerMark(player, "emo__kubaiwang_hp", 0)
  player.room:setPlayerMark(player, "emo__kubaiwang_card", 0)
  player.room:setPlayerMark(player, "emo__kubaiwang_cardData", 0)
end)

return skel
